package mongo;

import java.net.UnknownHostException;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.mongodb.util.JSON;

public class DeviceHandler {
	private Mongo mg = null;
    private DB db;
    private DBCollection devices;
    
    @Before
    public void init() {
        try {
            mg = new Mongo();
            //mg = new Mongo("localhost", 27017);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (MongoException e) {
            e.printStackTrace();
        }
        //获取temp DB；如果默认没有创建，mongodb会自动创建
        db = mg.getDB("ceshi");
        //获取devices DBCollection；如果默认没有创建，mongodb会自动创建
        devices = db.getCollection("devices");
    }
    
    @After
    public void destory() {
        if (mg != null)
            mg.close();
        mg = null;
        db = null;
        devices = null;
        System.gc();
    }
    
    @Test
    public void modify(){
    	Object obj = JSON.parse("[{\"name\":\"puding\"}]");
    	int num = devices.update(new BasicDBObject("sn", "8V40-DCBA-87654322"), 
    			new BasicDBObject("$set",new BasicDBObject("patchList", obj)), false, true).getN();
    	Assert.assertTrue(num>0);
    	
    }
    
}
